package com.irdstudio.efp.e4a.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.e4a.service.dao.SRoleDataRuleDao;
import com.irdstudio.efp.e4a.service.domain.SDataPers;
import com.irdstudio.efp.e4a.service.domain.SRoleDataRule;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.e4a.service.vo.SDataPersVO;
import com.irdstudio.efp.e4a.service.vo.SRoleDataRuleVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;

/**
 * <p>
 * ServiceImpl: 系统角色对应数据资源权限表
 * <p>
 * 
 * @author fuzm
 * @date 2018-05-03
 */
@Service("sRoleDataRuleService")
public class SRoleDataRuleServiceImpl implements SRoleDataRuleService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SRoleDataRuleServiceImpl.class);

	@Autowired
	private SRoleDataRuleDao sRoleDataRuleDao;

	/**
	 * 获取PERMIS_TYPE最大值
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,String> getDataRuleType(String dataPersId, List<String> roleCodes) {
		logger.debug("当前获取数据条件为:" + dataPersId + roleCodes);
		String str = null;
		Map<String,String> resultMap = new HashMap<String,String>();
		try {
			StringBuffer sb = new StringBuffer();
			if (roleCodes != null && roleCodes.size() > 0) {
				for (String newStr : roleCodes) {
					sb.append("'" + newStr + "'" + ",");
				}
				str = sb.substring(0, sb.length() - 1);
				resultMap = sRoleDataRuleDao.getDataRuleType(dataPersId, str);
			}

		} catch (Exception e) {
			logger.error("获取字符串数据发生异常!", e);

		}
		logger.debug("根据条件:" + dataPersId + roleCodes + "获取的字符串为" + str);
		return resultMap;
	}

	/**
	 * 新增操作
	 */
	@Override
	public int insertSRoleDataRule(SRoleDataRuleVO inSRoleDataRuleVo) {
		logger.debug("当前新增数据为:" + inSRoleDataRuleVo.toString());
		int num = 0;
		try {
			SRoleDataRule sRoleDataRule = new SRoleDataRule();
			beanCopy(inSRoleDataRuleVo, sRoleDataRule);
			SRoleDataRuleVO oldSRoleDataRule = queryRoleByPk(inSRoleDataRuleVo);
			if (oldSRoleDataRule != null) {
				// 已经存在此数据配置，不能重复新增
				num = -2;
			} else {
				num = sRoleDataRuleDao.insertSRoleDataRule(sRoleDataRule);
			}

		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteRoleByPk(SRoleDataRuleVO inSRoleDataRuleVo) {
		logger.debug("当前删除数据条件为:" + inSRoleDataRuleVo);
		int num = 0;
		try {
			SRoleDataRule sRoleDataRule = new SRoleDataRule();
			beanCopy(inSRoleDataRuleVo, sRoleDataRule);
			num = sRoleDataRuleDao.deleteRoleByPk(sRoleDataRule);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRoleDataRuleVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateRoleByPk(SRoleDataRuleVO inSRoleDataRuleVo) {
		logger.debug("当前修改数据为:" + inSRoleDataRuleVo.toString());
		int num = 0;
		try {
			SRoleDataRule sRoleDataRule = new SRoleDataRule();
			beanCopy(inSRoleDataRuleVo, sRoleDataRule);

			num = sRoleDataRuleDao.updateRoleByPk(sRoleDataRule);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRoleDataRuleVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public SRoleDataRuleVO queryRoleByPk(SRoleDataRuleVO inSRoleDataRuleVo) {

		logger.debug("当前查询参数信息为:" + inSRoleDataRuleVo);
		try {
			SRoleDataRule querySRoleDataRule = new SRoleDataRule();
			beanCopy(inSRoleDataRuleVo, querySRoleDataRule);
			SRoleDataRule queryRslSRoleDataRule = sRoleDataRuleDao.queryRoleByPk(querySRoleDataRule);
			if (Objects.nonNull(queryRslSRoleDataRule)) {
				SRoleDataRuleVO outSRoleDataRuleVo = beanCopy(queryRslSRoleDataRule, new SRoleDataRuleVO());
				logger.debug("当前查询结果为:" + outSRoleDataRuleVo.toString());
				return outSRoleDataRuleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleDataRuleVO> queryAllRoleOwner(SRoleDataRuleVO sRoleDataRuleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SRoleDataRule sRoleDataRule = beanCopy(sRoleDataRuleVo, new SRoleDataRule());
		List<SRoleDataRuleVO> list = null;
		try {
			List<SRoleDataRule> sRoleDataRules = sRoleDataRuleDao.queryAllRoleOwnerByPage(sRoleDataRule);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sRoleDataRules.size());
			pageSet(sRoleDataRules, sRoleDataRule);
			list = (List<SRoleDataRuleVO>) beansCopy(sRoleDataRules, SRoleDataRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleDataRuleVO> queryAllRoleCurrOrg(SRoleDataRuleVO sRoleDataRuleVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SRoleDataRule sRoleDataRule = beanCopy(sRoleDataRuleVo, new SRoleDataRule());
		List<SRoleDataRule> sRoleDataRules = sRoleDataRuleDao.queryAllRoleCurrOrgByPage(sRoleDataRule);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sRoleDataRules.size());
		List<SRoleDataRuleVO> list = null;
		try {
			pageSet(sRoleDataRules, sRoleDataRule);
			list = (List<SRoleDataRuleVO>) beansCopy(sRoleDataRules, SRoleDataRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleDataRuleVO> queryAllRoleCurrDownOrg(SRoleDataRuleVO sRoleDataRuleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRoleDataRule sRoleDataRule = beanCopy(sRoleDataRuleVo, new SRoleDataRule());
		List<SRoleDataRule> sRoleDataRules = sRoleDataRuleDao.queryAllRoleCurrDownOrgByPage(sRoleDataRule);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sRoleDataRules.size());
		List<SRoleDataRuleVO> list = null;
		try {
			pageSet(sRoleDataRules, sRoleDataRule);
			list = (List<SRoleDataRuleVO>) beansCopy(sRoleDataRules, SRoleDataRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	// 系统数据权限

	/**
	 * 新增操作
	 */
	@Override
	public int insertSDataPers(SDataPersVO inSDataPersVo) {
		logger.debug("当前新增数据为:" + inSDataPersVo.toString());
		int num = 0;
		try {
			SDataPers sDataPers = new SDataPers();
			beanCopy(inSDataPersVo, sDataPers);
			num = sRoleDataRuleDao.insertSDataPers(sDataPers);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteDataByPk(SDataPersVO inSDataPersVo) {
		logger.debug("当前删除数据条件为:" + inSDataPersVo);
		int num = 0;
		try {
			SDataPers sDataPers = new SDataPers();
			beanCopy(inSDataPersVo, sDataPers);
			num = sRoleDataRuleDao.deleteDataByPk(sDataPers);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSDataPersVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateDataByPk(SDataPersVO inSDataPersVo) {
		logger.debug("当前修改数据为:" + inSDataPersVo.toString());
		int num = 0;
		try {
			SDataPers sDataPers = new SDataPers();
			beanCopy(inSDataPersVo, sDataPers);
			num = sRoleDataRuleDao.updateDataByPk(sDataPers);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSDataPersVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public SDataPersVO queryDataByPk(SDataPersVO inSDataPersVo) {

		logger.debug("当前查询参数信息为:" + inSDataPersVo);
		try {
			SDataPers querySDataPers = new SDataPers();
			beanCopy(inSDataPersVo, querySDataPers);
			SDataPers queryRslSDataPers = sRoleDataRuleDao.queryDataByPk(querySDataPers);
			if (Objects.nonNull(queryRslSDataPers)) {
				SDataPersVO outSDataPersVo = beanCopy(queryRslSDataPers, new SDataPersVO());
				logger.debug("当前查询结果为:" + outSDataPersVo.toString());
				return outSDataPersVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SDataPersVO> queryAllDataOwner(SDataPersVO sDataPersVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SDataPers sDataPers = beanCopy(sDataPersVo, new SDataPers());
		List<SDataPersVO> list = null;
		try {
			List<SDataPers> sDataPerss = sRoleDataRuleDao.queryAllDataOwnerByPage(sDataPers);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sDataPerss.size());
			pageSet(sDataPerss, sDataPers);
			list = (List<SDataPersVO>) beansCopy(sDataPerss, SDataPersVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SDataPersVO> queryAllDataCurrOrg(SDataPersVO sDataPersVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SDataPers sDataPers = beanCopy(sDataPersVo, new SDataPers());
		List<SDataPers> sDataPerss = sRoleDataRuleDao.queryAllDataCurrOrgByPage(sDataPers);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sDataPerss.size());
		List<SDataPersVO> list = null;
		try {
			pageSet(sDataPerss, sDataPers);
			list = (List<SDataPersVO>) beansCopy(sDataPerss, SDataPersVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SDataPersVO> queryAllDataCurrDownOrg(SDataPersVO sDataPersVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SDataPers sDataPers = beanCopy(sDataPersVo, new SDataPers());
		List<SDataPers> sDataPerss = sRoleDataRuleDao.queryAllDataCurrDownOrgByPage(sDataPers);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sDataPerss.size());
		List<SDataPersVO> list = null;
		try {
			pageSet(sDataPerss, sDataPers);
			list = (List<SDataPersVO>) beansCopy(sDataPerss, SDataPersVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}
}
